package com.huffman.smart_picture.controller;

import com.huffman.smart_picture.common.auth.SessionContext;
import com.huffman.smart_picture.common.auth.annotation.LoginRequired;
import com.huffman.smart_picture.common.UserSession;
import com.huffman.smart_picture.common.base.reponse.ResponseResult;
import com.huffman.smart_picture.common.exception.ErrorEnum;
import com.huffman.smart_picture.domain.request.picture.PictureFavoriteRequest;
import com.huffman.smart_picture.service.FavoritePictureRecordService;
import com.huffman.smart_picture.utils.ResponseUtil;
import com.huffman.smart_picture.utils.ThrowUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author DengLin
 * @title 点赞控制器
 * @date 2025/5/6
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Resource
    private SessionContext sessionContext;

    @Resource
    private FavoritePictureRecordService favoritePictureByLuaService;


    @PostMapping("/picture/do")
    @ApiOperation(value = "图片点赞")
    @LoginRequired
    public ResponseResult doFavoritePicture(@RequestBody PictureFavoriteRequest pictureFavoriteRequest) {
        UserSession userSession = sessionContext.getUserSession();
        Long id = pictureFavoriteRequest.getId();
        ThrowUtil.throwIf(id == null || id < 1, ErrorEnum.PARAM_EMPTY, "图片id为空或非法");
        boolean res = favoritePictureByLuaService.doFavoritePicture(pictureFavoriteRequest,userSession);
        return ResponseUtil.success(res);
    }

    @PostMapping("/picture/undo")
    @ApiOperation(value = "图片点赞")
    @LoginRequired
    public ResponseResult undoFavoritePicture(@RequestBody PictureFavoriteRequest pictureFavoriteRequest) {
        UserSession userSession = sessionContext.getUserSession();
        Long id = pictureFavoriteRequest.getId();
        ThrowUtil.throwIf(id == null || id < 1, ErrorEnum.PARAM_EMPTY, "图片id为空或非法");
        boolean res = favoritePictureByLuaService.undoFavoritePicture(pictureFavoriteRequest,userSession);
        return ResponseUtil.success(res);
    }



}
